package com.province.platform.core.classLoader;

public final class ClassLoaderSwapper {
    private ClassLoader currentClassLoader = null;

    private ClassLoaderSwapper() {}

    public static ClassLoaderSwapper newInstance() {
        return new ClassLoaderSwapper();
    }

    /**
     * 保存当前classLoader
     *
     * @param classLoader
     * @return
     */
    public ClassLoader setClassLoader(ClassLoader classLoader) {
        this.currentClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        return this.currentClassLoader;
    }

    public ClassLoader restoreClassLoader() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(this.currentClassLoader);
        return classLoader;
    }
}
